package cn.restcloud.example.dao.impl;

import java.util.List;

import cn.restcloud.example.dao.IDemoUserDao;
import cn.restcloud.example.model.UserModel;
import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.base.IBasePageBean;
import cn.restcloud.framework.core.base.MongoBaseDao;
import cn.restcloud.framework.core.base.MongoPageParams;
import cn.restcloud.framework.core.base.RdbPageParams;
import cn.restcloud.framework.core.container.BeanFactory;

import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.BasicDBObject;
import com.mongodb.client.model.Filters;

import cn.restcloud.framework.core.util.RequestUtil;
import cn.restcloud.framework.core.util.db.mongo.MongoPageUtil;
import cn.restcloud.framework.core.util.db.mongo.MongoUtil;
import cn.restcloud.framework.core.util.db.rdb.RdbPageUtil;
import cn.restcloud.framework.core.util.db.rdb.RdbUtil;


/**
 * Dao MongoDB数据库实现类
 * @author Administrator
 *
 */

@BeanConfig(appId = "example",beanId = "DemoUserMongo",beanName = "Demo示例Dao实现类",beanType = BeanType.DAO,autowired = false,singleton = true,hotMode = true)
public class DemoUserMongoImpl extends MongoBaseDao<UserModel>  implements IDemoUserDao {


	/**
	 * 根据_id获取对像
	 * @param beanId
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserModel getById(String id) throws Exception {
		return MongoUtil.getBeanById(getCollection(), id, UserModel.class);
	}
	

	/**
	 * 保存对像
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@Override
	public int save(UserModel obj) throws Exception {
		Document doc = MongoUtil.save(getCollection(), obj);
		if(doc==null){
			return 0;
		}else{
			return 1;
		}
	}

	/**
	 * 根据Document的_id删除一条文档
	 * @param _id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteById(String _id) throws Exception {
		return MongoUtil.deleteById(getCollection(), _id);
	}
	
	/**
	 * 根据Document的_id删除多条文档,ids多个用逗号分隔
	 * @param _id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByIds(String _ids) throws Exception {
		int i=0;
		String[] idArray=_ids.split(",");
		for(String id:idArray){
			this.deleteById(id);
			i++;
		}
		return i;
	}
	
	/**
	 * 根据bean对像进行删除
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@Override
	public int delete(UserModel obj) throws Exception {
		return MongoUtil.deleteByBean(getCollection(), obj);
	}


	@Override
	public IBasePageBean<UserModel> listByPage() throws Exception {
		return MongoPageUtil.listByPage(this);
	}


	@Override
	public UserModel getByUserId(String userid) throws Exception {
		Bson filters=Filters.eq("userId",userid);
		return MongoUtil.getBean(getCollection(), filters, UserModel.class);
	}


	@Override
	public List<UserModel> listAll() throws Exception {
		return MongoUtil.listAllBeans(getCollection(), UserModel.class);
	}


	@Override
	public IBasePageBean<UserModel> listByPageAndState(String state) throws Exception {
		// 这里进行指定条件的数据查询并分页
		Bson filters=Filters.eq("state",state);
		MongoPageParams pageParams=MongoPageUtil.getPageParams(getCollection(), filters, "editTime", "ASC");
		IBasePageBean<UserModel> pageBean=super.listByPage(pageParams, UserModel.class);
		return pageBean;
	}

	@Override
	public IBasePageBean<UserModel> listByPageAndStateAndFilters(String state)
			throws Exception {
		// 这里进行指定条件的数据查询并分页
		Bson filters=Filters.eq("state",state);
		//最后一个参数true表示合并url中传入进来的filters,searchFilters参数中的条件
		MongoPageParams pageParams=MongoPageUtil.getPageParams(getCollection(), filters, "editTime", "ASC",true);
		IBasePageBean<UserModel> pageBean=super.listByPage(pageParams, UserModel.class);
		return pageBean;
	}

	@Override
	public IBasePageBean<UserModel> listByPageBySqlWhere(String state)
			throws Exception {
		//先创建一个pagebean对像
		IBasePageBean<UserModel> pageBean=(IBasePageBean<UserModel>)BeanFactory.getBean("PageBean");

		String urlPageNo=RequestUtil.getString("pageNo");//从url请求中获取当前页参数
		String urlPageSize=RequestUtil.getString("pageSize");//从url请求中获取分页显示数
		if(StringUtils.isBlank(urlPageSize)){urlPageSize="20";}
		if(StringUtils.isBlank(urlPageNo)){urlPageNo="1";}
		
		//设置默认的查询过虑条件功能如：fieldA=1 or fieldB=2
		Bson filters=Filters.eq("state",state);
		pageBean.setFilterBson(filters); //传入一个bson参数
		
		//设置默认的排序功能
		pageBean.setSortBson(new BasicDBObject("editTime", 1)); //升序
		
		//初始化分页数据
		int pageSize=Integer.parseInt(urlPageSize);
		if(pageSize==0){pageSize=DEFAULT_PAGESIZE;}
		pageBean.setPageNo(Integer.parseInt(urlPageNo)); //当前第几页
		pageBean.setPageSize(pageSize); //每页显示数
		MongoUtil.listBeansByPage(this.getCollection(), pageBean, UserModel.class);

		return pageBean; //返回分页结果
	}


}
